package com.lanqiao.controller;

import cn.hutool.json.JSONUtil;
import com.lanqiao.entity.Shufflinginfo;
import com.lanqiao.service.ShufflingService;
import com.lanqiao.service.impl.ShufflingSerImpl;
import org.lanqiao.mvc.entity.ModelAndView;
import org.lanqiao.mvc.entity.YockMvcAnnotation;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@YockMvcAnnotation.Controller("/shuffling")
public class ShufflingController {

    //轮播图业务
  private   ShufflingService shuffling = new ShufflingSerImpl();


        @YockMvcAnnotation.RequestMapping("/selectshufflingpic.do")
        @YockMvcAnnotation.ResponseDispatch("/WEB-INF/view/a.jsp")
        public ModelAndView selectshufflingpic(
                HttpServletRequest request,
                HttpServletResponse response
        ) throws Exception{
            ModelAndView model = new ModelAndView();

            return model;
        }


    /**
     * 查询轮播图片
     * @param request
     * @param response
     * @return
     */
    @YockMvcAnnotation.RequestMapping("/selectpic.do")
    @YockMvcAnnotation.ResponseBody
    public String uploadPicture(
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        //告诉前端返回来的是一个json数据
        response.setContentType("application/json;charset=utf-8");
        // 保存图片名称的集合
        Map<String, Object> map = new HashMap<>();
        //查询轮播图sql
        List<Shufflinginfo> selectshufflingpic = shuffling.selectshufflingpic();

        map.put("code",200);
        map.put("data",selectshufflingpic);

        String jsonStr = JSONUtil.toJsonStr(map);


        return jsonStr;
    }


    /**
     * 添加轮书本的轮播图
     * @param request
     * @param response
     * @throws Exception
     */
    @YockMvcAnnotation.RequestMapping("/addshuffling.do")	//配置二级路径
    @YockMvcAnnotation.ResponseVoid
    public void addshuffling(
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception{

        String book_id2 = request.getParameter("book_id");
        String ad_img = request.getParameter("ad_img");
        System.out.println("book_id2:" +book_id2);
        System.out.println("ad_img:" +ad_img);

        int book_id=0;
        if (book_id2!=null){
             book_id = Integer.parseInt(book_id2);
        }

        //添加图书的轮播图
        shuffling.addshuffling(ad_img,book_id);

    }






}
